package com.scs.application.interceptor;

import com.alibaba.fastjson.JSON;
import com.scs.application.modules.fsd.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 智能柜请求参数校验
 */
public class FsdInterceptor implements HandlerInterceptor {
    @Autowired
    StationService stationService;
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        String config = httpServletRequest.getHeader("Intellic-configuration");// 从 http 请求头中取出站点信息
        if (config == null) {
            throw new RuntimeException("站点配置无效！");
        }
        if (!stationService.checkStation(JSON.parseObject(config).getString("stationCode")))
            throw new RuntimeException("站点编号错误！");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
